#include "app_runner.h"
#include <signal.h>
#include "app_device.h"
#include "app_pool.h"
#include "app_mqtt.h"
#include "log.h"
#include <unistd.h>
#include "app_bt.h"
#include "app_serial.h"

static int is_running=1;

static void  app_runner_stop (int signal)
{
    is_running=0;
}

/**
 * @brief  业务主程序启动流程
 */
void  app_runner_run()
{
    signal(SIGINT, app_runner_stop); //中断信号 ctrl + c
    signal(SIGTERM, app_runner_stop); //终止信号  kill (不包括 kill-9)

    // 初始化mqtt 
    int ret= app_mqtt_init();
    if(ret==0)
    {
       log_info("mqtt init success");
    }
 
     // 初始化线程池
     ret= app_pool_init();
     if(ret==0)
     {
         log_info("pool init success");
     }
 
     // 初始化设备
     app_device_t *device= app_device_init();
  
   
     if(device!=NULL)
     {   //串口蓝牙
         app_serial_init(device);
         app_bt_init(device);
         log_info("device init success");
         //启动设备
         app_device_start();
     }
     while(is_running)
     {
         sleep(1);
     }
     // 资源释放阶段
     app_device_destroy();
     app_pool_destroy();
     app_mqtt_destroy();
     log_info("资源已释放,程序退出");
    
}